﻿CREATE TABLE [dbo].[FormControls] (
    [FormControlID]                 INT            IDENTITY (1, 1) NOT NULL,
    [ControlName]                   NVARCHAR (MAX) NULL,
    [Properties]                    NVARCHAR (MAX) NULL,
    [ParentControlID]               INT            NULL,
    [FormID]                        INT            NOT NULL,
    [FormControl_FormControlID]     INT            NULL,
    [ParentControl_FormControlID]   INT            NULL,
    [RootFormControl_FormControlID] INT            NULL,
    [Sort]                          INT            DEFAULT ((0)) NULL,
    [ClassName]                     NVARCHAR (MAX) NULL,
    CONSTRAINT [PK_dbo.FormControls] PRIMARY KEY CLUSTERED ([FormControlID] ASC),
    CONSTRAINT [FK_dbo.FormControls_dbo.FormControls_FormControl_FormControlID] FOREIGN KEY ([FormControl_FormControlID]) REFERENCES [dbo].[FormControls] ([FormControlID]),
    CONSTRAINT [FK_dbo.FormControls_dbo.FormControls_ParentControl_FormControlID] FOREIGN KEY ([ParentControl_FormControlID]) REFERENCES [dbo].[FormControls] ([FormControlID]),
    CONSTRAINT [FK_dbo.FormControls_dbo.FormControls_RootFormControl_FormControlID] FOREIGN KEY ([RootFormControl_FormControlID]) REFERENCES [dbo].[FormControls] ([FormControlID]),
    CONSTRAINT [FK_dbo.FormControls_dbo.Forms_FormID] FOREIGN KEY ([FormID]) REFERENCES [dbo].[Forms] ([FormID])
);


GO
CREATE NONCLUSTERED INDEX [IX_FormID]
    ON [dbo].[FormControls]([FormID] ASC);


GO
CREATE NONCLUSTERED INDEX [IX_FormControl_FormControlID]
    ON [dbo].[FormControls]([FormControl_FormControlID] ASC);


GO
CREATE NONCLUSTERED INDEX [IX_ParentControl_FormControlID]
    ON [dbo].[FormControls]([ParentControl_FormControlID] ASC);


GO
CREATE NONCLUSTERED INDEX [IX_RootFormControl_FormControlID]
    ON [dbo].[FormControls]([RootFormControl_FormControlID] ASC);

